<?php

	include_once '../modelo/sections_model.php'; 
	
	class ControladorSecciones {
		
		public $insertString = "INSERT INTO SECTIONS(APP_ID,SEC_NAME,SEC_DATE_CREATED,SEC_DATE_UPDATED) VALUES ";
		public $query = "";		
		public $seccModel = null;
		
		function __construct(){
			$this->seccModel = new SectionsModel();			
		}
		
		function sectionCreate(Section $secc){			
					
				$query = $this->insertString ."(".$secc->getAppId().",'".$secc->getSecName()."','".$secc->getSecCreatedDate()."','".$secc->getSecUpdatedDate()."')";
						
			return $this->seccModel->createSection($query);
		}
		
			function searchSectionId($app_id,$name_section){											
			
			$query = "SELECT B.SEC_ID FROM APPLICATIONS A, SECTIONS B WHERE A.APP_ID = B.APP_ID AND B.SEC_NAME = '".$name_section."' AND A.APP_ID = ".$app_id;
			
			$select = $this->seccModel->searchSectionId($query);
			
			while ($row = $select)
				  return $row[0];	 									  	
		}
		
		function searchSectionName($app_id,$sec_id){
			
			$query = "SELECT B.SEC_NAME FROM APPLICATIONS A, SECTIONS B WHERE A.APP_ID = B.APP_ID AND B.SEC_ID = ".$sec_id." AND A.APP_ID = ".$app_id;
			
			$select = $this->seccModel->searchSectionId($query);
			
			while ($row = $select)
				  return $row[0];	 									  	
			
		}
		
		
		function searchApplicationSections($app_id,$usr_id){											
			
			$query = "SELECT B.* FROM APPLICATIONS A, SECTIONS B WHERE A.APP_ID = B.APP_ID AND A.APP_ID = ".$app_id;			
			$select = $this->seccModel->searchApplicationSections($query);
			
			$sections = '<ul> <form action="" name="formSections" id="formSections" method="post">';
			
				while ($row = pg_fetch_row($select)) {
						set_time_limit(0);
					 if ($row[2] != "Bienvenida")
					$sections = $sections . '<li><a href="section_elements.php?app='.$app_id.'&sec='.$row[0].'&usr='.$usr_id.'">'.$row[2].'</a> <img src="../imagenes/removeInput.png" onClick="deleteSection('.$row[0].')"/></li>';		
					else
					$sections = $sections . '<li><a href="section_elements.php?app='.$app_id.'&sec='.$row[0].'&usr='.$usr_id.'">'.$row[2].'</a> </li>';		
				}
				
			$sections = $sections . '</form> </ul>';
			return $sections;									  	
		}
		
		function searchApplicationSectionsName($app_id){
			$query = "SELECT B.* FROM APPLICATIONS A, SECTIONS B WHERE A.APP_ID = B.APP_ID AND A.APP_ID = ".$app_id;			
			$select = $this->seccModel->searchApplicationSections($query);
			
			$sections = array();
			$i = 0;			
				while ($row = pg_fetch_row($select)) {
						set_time_limit(0);						
					$section = new Section($app_id,$row[2],'','');
					$section->setSecId($row[0]);
						$sections[$i] = $section;
						$i = $i + 1;
				}
			return $sections;									  	
		}
	}

?>